<div class="row">
<div class="col-xs-12">
	<div class="box">
		<div class="box-header">
			<form id="auto-query-form" class="form-inline" method="post" role="form">
				<div class="form-group">
					<label class="control-label" for="query.keyword">关键字</label>
					<input id="query.keyword" name="query.keyword" placeholder="请输入关键字" class="form-control"/>
					<input id="query.type" name="query.type" value="#(query.type)" type="hidden"/>
				</div>
				<div class="form-group">
					<label class="control-label" for="query.stime">时间</label>
					<input id="query.stime" name="query.stime" value="#(query.stime??)" placeholder="时间始" class="form-control date" datetimepicker="yyyy-mm-dd" maxlength="19"/>
					<label class="control-label" for="query.etime">至</label>
					<input id="query.etime" name="query.etime" value="#(query.etime??)" placeholder="时间止" class="form-control date" datetimepicker="yyyy-mm-dd" maxlength="19"/>
				</div>	
				<div class="form-group">
					#@query_btn()
				</div>
			</form>
		</div>

		<div class="box-body">
			<table id="auto_data_table" class="table table-striped table-hover table-bordered dataTable no-footer" width="100%">
			</table>
		</div>

		<div class="box-footer text-center">
			<a id="create_btn" href="javascript:void(0)" class="btn btn-success" data-uri="msg/index"> <i class="fa fa-plus"></i>&nbsp;&nbsp;新建邮件</a>
			<a id="delete_btn" href="javascript:void(0)" class="btn btn-danger" data-uri="msg/index"> <i class="fa fa-trash"></i>&nbsp;&nbsp;删除</a>
		</div>
	</div>
</div>
</div>
<script type="text/javascript">
	var boxtype=jid("query.type").val();
	var dtSelf = null;
	var columnDef =  [
		   { "data" : "id", "name" : "id", "title" : "重要","width": 30 ,"sClass":"text-center" ,"orderable":false, render : function(data, type, row) {
			   var s = "";
			   if(row.flag=="1"){
				   s = "<i class='fa fa-star text-yellow'></i>";
			   }
                return s;
            } },
		   { "data" : "id", "name" : "id", "title" : "附件","width": 30 ,"sClass":"text-center" ,"orderable":false, render : function(data, type, row) {
			   var s = "";
			   if(row.atta=="1"){
				   s = "<i class='fa fa-paperclip'></i>";
			   }
                return s;
            } },
		   { "data" : "to_name", "name" : "to_name", "title" : "收件人","width": 110 },
		   { "data" : "subject", "name" : "subject", "title" : "主题" , render : function(data, type, row) {
			   if(null==row.read_time){
				   data="<i class='unread'>"+data+"</i>";
			   }
                return data;
            } },
		   { "data" : "ctime", "name" : "ctime", "title" : "时间","width": 100 ,"sClass":"text-center" , render : function(data, type, row) {
                return timeago(data);
            } }
		];
	if("01"==jid("query.type").val()){//收件箱
		columnDef[2] = { "data" : "from_name", "name" : "from_id", "title" : "发件人","width": 110 };
	}
	$(document).ready(function() {
			dtSelf = $('#auto_data_table').autoDataTable({
			controller : "msg",
			title : "系统邮件",
			columns :columnDef,
			fnInitComplete : function(oSettings){
				$("#auto_data_table tbody").on("click", "tr", function() {
					$(this).toggleClass("selected");
				}).on( "dblclick", "tr", function(e) {
					$($("#auto_data_table").dataTable().fnGetNodes(this)).find(".unread").removeClass("unread");
					var $row = $("#auto_data_table").dataTable() .fnGetData(this);
					var tempId = $row["id"];
					if("03"==jid("query.type").val()){//草稿箱，直接编辑
						openMsgForm("edit/" + tempId, "重新发送" );
						return;
					}
					var buttons = [ {
						label : '回复',
						cssClass : 'btn-danger btn-sm',
						icon : 'fa fa-trash',
						action : function(dialog) {
							dialog.close();
							openMsgForm("reply/" + tempId ,"回复邮件" );
						}
					},{
						label : '转发',
						cssClass : 'btn-danger btn-sm',
						icon : 'fa fa-trash',
						action : function(dialog) {
							dialog.close();
							openMsgForm("forward/" + tempId, "转发邮件" );
						}
					},{
						label : '删除',
						cssClass : 'btn-danger btn-sm',
						icon : 'fa fa-trash',
						action : function(dialog) {
							confirmDialog("<strong class='text-danger'>确定删除选中邮件记录吗？</strong>", function() {
								var delurl = "msg/delete/" + tempId;
								postData(delurl, {type:jid("query.type").val()},null, function(data) {
									if (data.success) {
										dialog.close();
										dtSelf.fnDraw();
									}
								});
							});
						}
					},{
						label : '打印',
						cssClass : 'btn-success btn-sm',
						icon : 'fa fa-print',
						action : function(dialog) {
							var printWarp = $(dialog.getModalBody()).find("form#auto-show-form");
							if(printWarp.length < 1){
								printWarp = $(dialog.getModalBody()).find(".printArea");
							}
							printWarp.printArea({extraCss: "assets/plugin/printArea/PrintArea.css"});
						}
					}, {
						label : '关闭',
						cssClass : 'btn-info btn-sm',
						icon : 'fa fa-window-close',
						action : function(dialog) {
							dialog.close();
						}
					} ];
					var dialog = loadUriDialog("查看邮件" ,  "msg/get/" + tempId, null, 800, null, buttons, function(dialog) {

							});
				});
			}
		});
		//新建邮件
		$("#create_btn").click(function(e) {
			openMsgForm("add","新建邮件");
		});
		// 删除数据
		$('#delete_btn').click(
				function(e) {
					e.preventDefault();
					var rows = getSelectedMsg();
					if (null == rows || rows.length < 1) {
						showMsg("请单击选择邮件数据行！", "error");
						return;
					}
					confirmDialog("<strong class='text-danger'>确定删除选中的" + rows.length + "条系统邮件吗？</strong>", function() {
						var ids = "";// 获取当前选中行ID
						for (var i = 0; i < rows.length; i++) {
							if ("" != ids){ids += "-";}
							ids += rows[i].id;
						}
						var url ="msg/delete/" + ids;
						postData(url, {type:jid("query.type").val()},null, function(data) {
							if (data.success) {
								dtSelf.fnDraw();
							}
						});
					});
				});
	});
	
	function getSelectedMsg() {
		var rows = [];
		if (null != dtSelf) {
			$("#auto_data_table tr.selected").each(function() {
				rows.push(dtSelf.fnGetData(this));
			});
		}
		return rows;
	};
	
	function openMsgForm(action,tip){
		var buttons = [ {
			label : '立即发送',
			cssClass : 'btn-warning btn-sm',
			icon : 'fa fa-envelope-o',
			action : function(dialog) {
				postMsgForm(dialog,"sent");
			}
		}, {
			label : '保存草稿',
			cssClass : 'btn-success btn-sm',
			icon : 'fa fa-pencil',
			action : function(dialog) {
				postMsgForm(dialog,"draft");
			}
		}, {
			label : '取消',
			cssClass : 'btn-info btn-sm',
			icon : 'fa fa-window-close',
			action : function(dialog) {
				dialog.close();
			}
		} ];
		loadUriDialog(tip, "msg/"+action, null, 800,null, buttons, function(dialog) {
		});
	}
	function postMsgForm(dialog,action){
		$("#msg_action").val(action);
		postValidatorForm("auto-data-form", function(data) {
			if (data.success) {
				dtSelf.fnDraw();
				closeDialog();
			}
		});
	}
</script>